package itheima.edu.info.manager.dao;

import itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

//库管
public class StudentDao {
    //1.创建存储学生对象的容器
    // private static Student[] stus = new Student[5];
    private static ArrayList<Student> list = new ArrayList<>();

    public boolean addStudent(Student stu) {
       list.add(stu);
            return true;
    }

    public Student[] findAllStudent() {
        Student [] stu =new Student[list.size()];
        for (int i = 0; i < list.size(); i++) {
            stu[i]=list.get(i);
        }
        return stu;
    }

    public void deleteStudentById(String delId) {
        //1.查找id在容器中所在的索引位置
        int index = getIndex(delId);

        //2.将该索引位置，使用null元素覆盖
       // stus[index]=null;
        list.set(index,null);
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        //1.查找updateId,在容器中的索引位置
        int index=getIndex(updateId);
        //2.将该索引位置，使用新的学生对象替换
        // stus[index]=newStu;
        list.set(index,newStu);
    }

}
